<?php 

	require('./Gen_Scripts/gen_functions.php');	
/*************************************************************
                    Tourny Stuff
 *************************************************************/

function show_game_tourny($tid, $avsb, $game_id, $game_gp){
	dblogin();
	$query =
	'select game, type, num_per_team, team_per_game, bring1, bring2, bring3, bring4, bring1_num, bring2_num, bring3_num, bring4_num,
		rule1_type, rule2_type, rule3_type
	   from GAMES g where g.gameid = '.$game_id;
	if($result = mysql_query($query)){
		if($row = mysql_fetch_array($result)){
		 	$game = $row['game'];
			$type = $row['type'];
			$num_per_team = $row['num_per_team'];
			$team_per_game = $row['team_per_game'];
			$bring1 = $row['bring1'];
			$bring2 = $row['bring2'];
			$bring3 = $row['bring3'];
			$bring4 = $row['bring4'];
			$bring1_need = $row['bring1_num'];
			$bring2_need = $row['bring2_num'];
			$bring3_need = $row['bring3_num'];
			$bring4_need = $row['bring4_num'];
			$rule1_type = $row['rule1_type'];
			$rule2_type = $row['rule2_type'];
			$rule3_type = $row['rule3_type'];
		}
	}

	/* Count up the Votes!! */
	$queryt = 
	'select sum(if(vote = \'A\',1,0)) as "VOTE_A",
			sum(if(vote=\'B\',1,0)) as "VOTE_B"
	 from TOURNY_VOTE t
	 where t.t_id = '.$tid.'
	   and t.rsvp = \'Y\'';
	 if($resultt = mysql_query($queryt)){
		if($rowt = mysql_fetch_array($resultt)){
			$votea = $rowt['VOTE_A'];
			$voteb = $rowt['VOTE_B'];
		}
	}
	if($avsb == 'A'){
		$game_status = $votea*20;
	}else{
		$game_status = $voteb*20;
	}		

	/*  Which one have I voted for? */
	$querym = 
	'select vote
	 from TOURNY_VOTE t
	 where t.t_id = '.$tid.'
	   and t.player = '.$_SESSION['USERNUM'];
	 
	 if($resultm = mysql_query($querym)){
		if($rowm = mysql_fetch_array($resultm)){
			$vote = $rowm['vote'];
		}
	}

	/* Teams */
	$i = 0;
	$num_teams = 2;
	$j = 0;

$num_per_team = 1;



	show_bringing($tid, $avsb, $gameid, $bring1, $bring2, $bring3, $bring4, $bring1_need, $bring2_need, $bring3_need, $bring4_need);

echo '	<div id="border" class = "',$avsb; if($vote == $avsb)echo ' selected '; echo ' ">';
echo '		<div id="vote_game" class=" ',$type;if(loggedin())echo ' vote pointer '; echo ' " name = "'.$avsb.'" >';
echo '			<h1>'.$game.'</h1>
			</div>';
echo '		<div class="'.$rule1_type.' hover" dir="rules.php?gameid=',$game_id,'&rule=1" id="rule_info">'.$rule1_type.'</div>
			<div class="'.$rule2_type.' hover" dir="rules.php?gameid=',$game_id,'&rule=2" id="rule_info">'.$rule2_type.'</div>
			<div class="'.$rule3_type.' hover" dir="rules.php?gameid=',$game_id,'&rule=3" id="rule_info">'.$rule3_type.'</div>
			<div class="single hover"          dir="details.php?gameid=',$game_id,'" id="team_info">'.$num_per_team.':'.$team_per_game.'</div>
			<div class="hover"                 dir="gp.php?gameid=',$game_id,'&tid=',$tid,'" id="gamepoint_info">&THORN;</div>';
echo '	</div>';
/** Display the status of the Vote **/

echo '	<div id="votestat" class="',$avsb,'">
			<div id="status" class="localhover"	title="',$game_status/20,'" style="width: '.$game_status.'px"></div>
		</div>';
		
		/**   Team Stuff **
		if(loggedin() && $num_per_team > 1){
echo '	<div id="team">
			<h1>Team management</h1>';
			while($i < $num_teams){
echo '			<div id="group">';
				while ($j < $num_per_team){
echo '				<input type="text"  value="'.$player[i][j].'" DISABLED>';
				}
echo '			</div>';
			}
echo '		<input class="add" type="submit" value="+">
		</div>';
	} */
}

function show_bringing($tid, $avsb, $gameid, $bring1, $bring2, $bring3, $bring4, $bring1_need, $bring2_need, $bring3_need, $bring4_need){

	dblogin();
	$bring1_me = 0;$bring1_tot = 0;
	$bring2_me = 0;$bring2_tot = 0;
	$bring3_me = 0;$bring3_tot = 0;
	$bring4_me = 0;$bring4_tot = 0;

	/* Count up the Bringings!! */
	$querytb = 
	'select sum(if(bring_num = 1 , bring_val, 0)) as "TOT_BRING1",
			sum(if(bring_num = 2 , bring_val, 0)) as "TOT_BRING2",
			sum(if(bring_num = 3 , bring_val, 0)) as "TOT_BRING3",
			sum(if(bring_num = 4 , bring_val, 0)) as "TOT_BRING4"
	 from BRING_VOTE t
	 where t.t_id = '.$tid.'
	   and t.game_no = \''.$avsb.'\'';
	 if($resulttb = mysql_query($querytb)){
		if($rowtb = mysql_fetch_array($resulttb)){
			if($rowtb['TOT_BRING1'] != ''){
				$bring1_tot = $rowtb['TOT_BRING1'];
			}
			if($rowtb['TOT_BRING2'] != ''){
				$bring2_tot = $rowtb['TOT_BRING2'];
			}
			if($rowtb['TOT_BRING3'] != ''){
				$bring3_tot = $rowtb['TOT_BRING3'];
			}
			if($rowtb['TOT_BRING4'] != ''){
				$bring4_tot = $rowtb['TOT_BRING4'];
			}
		}
	}
	/* Count up the Bringings!! */
	$querymb = 
	'select sum(if(bring_num = 1 , bring_val, 0)) as "ME_BRING1",
			sum(if(bring_num = 2 , bring_val, 0)) as "ME_BRING2",
			sum(if(bring_num = 3 , bring_val, 0)) as "ME_BRING3",
			sum(if(bring_num = 4 , bring_val, 0)) as "ME_BRING4"
	 from BRING_VOTE t
	 where t.t_id = '.$tid.'
	   and t.game_no = \''.$avsb.'\'
	   and t.player = '.$_SESSION['USERNUM'];
	   
	 if($resultmb = mysql_query($querymb)){
		if($rowmb = mysql_fetch_array($resultmb)){
			if($rowmb['ME_BRING1'] != ''){
				$bring1_me = $rowmb['ME_BRING1'];
			}
			if($rowmb['ME_BRING2'] != ''){
				$bring2_me = $rowmb['ME_BRING2'];
			}
			if($rowmb['ME_BRING3'] != ''){
				$bring3_me = $rowmb['ME_BRING3'];
			}
			if($rowmb['ME_BRING4'] != ''){
				$bring4_me = $rowmb['ME_BRING4'];
			}
		}
	}

echo '  <div id="bringing" class="'.$avsb.'">';
if(loggedin()){
echo '		<h1> What to bring </h1>';

if($bring1 != ''){
echo "		<div id='item'>
				<input class = 'pointer submit bring' name ='1".$avsb."' type='submit' value='-'>
					<div id='title'>".$bring1."</div>
				<input class = 'pointer submit bring' name ='1".$avsb."' type='submit' value='+'>
				<input class='num' type='text' value='".$bring1_me."'>
				<input class='num' type='text' value='".$bring1_tot."'>
				<input class='num' type='text' value='".$bring1_need."'>
			</div>";
} 
if($bring2 != ''){
echo "		<div id='item'>
				<input class = 'pointer submit bring' name ='2".$avsb."' type='submit' value='-'>
					<div id='title'>".$bring2."</div>
				<input class = 'pointer submit bring' name ='2".$avsb."' type='submit' value='+'>
				<input class='num' type='text' value='".$bring2_me."'>
				<input class='num' type='text' value='".$bring2_tot."'>
				<input class='num' type='text' value='".$bring2_need."'>
			</div>";
}
if($bring3 != ''){
echo "		<div id='item'>
				<input class = 'pointer submit bring' name ='3".$avsb."' type='submit' value='-'>
					<div id='title'>".$bring3."</div>
				<input class = 'pointer submit bring' name ='3".$avsb."' type='submit' value='+'>
				<input class='num' type='text' value='".$bring3_me."'>
				<input class='num' type='text' value='".$bring3_tot."'>
				<input class='num' type='text' value='".$bring3_need."'>
			</div>";
}
if($bring4 != ''){
echo "		<div id='item'>
				<input class = 'pointer submit bring' name ='4".$avsb."' type='submit' value='-'>
				<div id='title'>".$bring4."</div>
				<input class = 'pointer submit bring' name ='4".$avsb."' type='submit' value='+'>
				<input class='num' type='text' value='".$bring4_me."'>
				<input class='num' type='text' value='".$bring4_tot."'>
				<input class='num' type='text' value='".$bring4_need."'>
			</div>";
	}
}
echo '</div>';
}




?>